package com.cap.dreamcircle.View;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Bean.AnonymousResponseBean;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.IUserModel;
import com.cap.dreamcircle.Model.Impl.UserModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.MD5Utils;
import com.cap.dreamcircle.Utils.PreferenceUtils;
import com.cap.dreamcircle.Utils.RxCountDown;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.Utils.TransformUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.Subscription;
import rx.functions.Action0;

/**
 * Created by liucxu on 2017/7/12.
 */

public class PhoneRegisterActivity extends BaseActivity {

    @BindView(R.id.imv_back)
    ImageView imv_back;
    @BindView(R.id.edt_phone_num)
    EditText edt_phone_num;
    @BindView(R.id.tv_get_register_code)
    TextView tv_get_register_code;
    @BindView(R.id.edt_validate_code)
    EditText edt_validate_code;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;

    private IUserModel userModel;

    private Subscription subscription;

    @Override
    public int getLayoutId() {
        return R.layout.activity_phone_register;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
    }

    @Override
    public void setupData() {
        userModel = new UserModelImpl();
    }

    @OnClick({R.id.tv_get_register_code, R.id.tv_confirm, R.id.imv_back})
    void doClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_get_register_code:
                if (TextUtils.isEmpty(edt_phone_num.getText().toString())) {
                    ToastUtils.showShortToast(PhoneRegisterActivity.this, "请输入手机号码");
                } else {
                    getVerificationCode();
                }
                break;
            case R.id.tv_confirm:
                if (TextUtils.isEmpty(edt_phone_num.getText().toString())) {
                    ToastUtils.showShortToast(PhoneRegisterActivity.this, "请输入手机号码");
                } else {
                    if (TextUtils.isEmpty(edt_validate_code.getText().toString())) {
                        ToastUtils.showShortToast(PhoneRegisterActivity.this, "请输入验证码");
                    } else {
                        CreatePhoneUserAccount();
                    }
                }
                break;
            case R.id.imv_back:
                App.GetInstance().finishActivity(PhoneRegisterActivity.this);
                break;
        }
    }

    private void getVerificationCode() {
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", edt_phone_num.getText().toString());
        params.put("verify", MD5Utils.GetMD5Value("sendVerificationDreamSecurityData" + edt_phone_num.getText().toString()));
        userModel.GetVerificationCode(params).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
                tv_get_register_code.setEnabled(false);
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                hideProgress();
                tv_get_register_code.setEnabled(false);
                subscription= RxCountDown.countdown(60).subscribe(new Observer<Integer>() {
                    @Override
                    public void onCompleted() {
                        tv_get_register_code.setEnabled(true);
                        tv_get_register_code.setText("发送");
                    }

                    @Override
                    public void onError(Throwable e) {
                        tv_get_register_code.setEnabled(true);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        tv_get_register_code.setEnabled(false);
                        tv_get_register_code.setText(integer+"S");
                    }
                });
            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                ToastUtils.showShortToast(PhoneRegisterActivity.this,response.getDesc());
                tv_get_register_code.setEnabled(true);
            }
        });
    }

    private void CreatePhoneUserAccount() {
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", edt_phone_num.getText().toString());
        params.put("verification", edt_validate_code.getText().toString());
        userModel.CreatePhoneUserAccount(params).compose(TransformUtils.<AnonymousResponseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
            }
        }).subscribe(new CommonSubscriber<AnonymousResponseBean>() {
            @Override
            public void onSuccess(AnonymousResponseBean response) {
                hideProgress();
                PreferenceUtils.SaveUserToken(response.getToken());
                PreferenceUtils.SaveUserId(response.getUserId());
                App.GetInstance().finishClassActivity(DeviceRegisterActivity.class);
                Intent intent=new Intent(PhoneRegisterActivity.this,CompleteUserInfoActivity.class);
                startActivity(intent);
                App.GetInstance().finishActivity(PhoneRegisterActivity.this);
            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                ToastUtils.showShortToast(PhoneRegisterActivity.this, response.getDesc());
                tv_get_register_code.setEnabled(true);
            }
        });
    }

    @Override
    protected void onStop() {
        if(subscription!=null){
            subscription.unsubscribe();
        }
        super.onStop();
    }
}
